package com.yfq.gateway.apigatewaysdk.domain.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.yfq.gateway.apigatewaysdk.common.GatewayException;
import com.yfq.gateway.apigatewaysdk.common.Result;
import com.yfq.gateway.apigatewaysdk.config.GatewaySdkProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yfq
 * @date 2024/1/6 10:24
 * @description
 */
public class GatewayCenterService {
    private final Logger logger = LoggerFactory.getLogger(GatewayCenterService.class);

    private final GatewaySdkProperties properties;

    public GatewayCenterService(GatewaySdkProperties properties) {
        this.properties = properties;
    }

    /**
     * 注册应用
     *
     * @param systemId       应用id
     * @param systemName     应用名
     * @param systemType     应用类型
     * @param systemRegistry 应用注册地址
     */
    public void doRegistryApplication(String systemId, String systemName, String systemType, String systemRegistry) {
        Map<String, Object> param = new HashMap<>();
        param.put("systemId", systemId);
        param.put("systemName", systemName);
        param.put("systemType", systemType);
        param.put("systemRegistry", systemRegistry);
        String res;
        try {
            res = HttpUtil.get(properties.getAddress() + "/wg/admin/application/registerApplication", param, 3000);
        } catch (Exception e) {
            logger.error("应用注册超时，systemId：{}，systemName：{}，systemType：{}，systemRegistry：{}", systemId, systemName, systemType, systemRegistry, e);
            throw e;
        }

        logger.info("应用注册结果：{}", res);
        Result result = JSON.parseObject(res, Result.class);
        if (result.getCode() != 200) {
            throw new GatewayException("应用注册失败：" + result.getInfo());
        }
    }

    /**
     * 注册接口
     *
     * @param systemId         系统id
     * @param interfaceId      接口id
     * @param interfaceName    接口名
     * @param interfaceVersion 接口版本
     */
    public void doRegistryInterface(String systemId, String interfaceId, String interfaceName, String interfaceVersion) {
        Map<String, Object> param = new HashMap<>();
        param.put("systemId", systemId);
        param.put("interfaceId", interfaceId);
        param.put("interfaceName", interfaceName);
        param.put("interfaceVersion", interfaceVersion);
        String res;
        try {
            res = HttpUtil.get(properties.getAddress() + "/wg/admin/application/registerInterface", param, 3000);
        } catch (Exception e) {
            logger.error("接口注册超时：systemId：{}，interfaceId：{}，interfaceName：{}，interfaceVersion：{}", systemId, interfaceId, interfaceName, interfaceVersion, e);
            throw e;
        }

        Result result = JSON.parseObject(res, Result.class);
        if (result.getCode() != 200) {
            throw new GatewayException("接口注册失败" + result.getInfo());
        }
    }

    /**
     * 注册方法
     *
     * @param systemId        系统id
     * @param interfaceId     接口id
     * @param methodId        方法id
     * @param methodName      方法名
     * @param parameterType   参数类型
     * @param uri             uri
     * @param httpCommandType http请求方法类型
     * @param auth            是否需要授权
     */
    public void doRegistryMethod(String systemId, String interfaceId, String methodId, String methodName,
                                 String parameterType, String uri, String httpCommandType, Integer auth) {
        Map<String, Object> param = new HashMap<>();
        param.put("systemId", systemId);
        param.put("interfaceId", interfaceId);
        param.put("methodId", methodId);
        param.put("methodName", methodName);
        param.put("parameterType", parameterType);
        param.put("uri", uri);
        param.put("httpCommandType", httpCommandType);
        param.put("auth", auth);
        String res;
        try {
            res = HttpUtil.get(properties.getAddress() + "/wg/admin/application/registerInterfaceMethod", param, 3000);
        } catch (Exception e) {
            logger.error("方法注册超时：systemId：{}，interfaceId：{}，methodId：{}，" +
                    "methodName：{}，parameterType：{}，uri：{}，httpCommandType：{}，auth：{}", systemId, interfaceId, methodId, methodName, parameterType, uri, httpCommandType, auth, e);
            throw e;
        }

        Result result = JSON.parseObject(res, Result.class);
        if (result.getCode() != 200) {
            throw new GatewayException("方法注册失败" + result.getInfo());
        }
    }

}
